package application.gui.clientWindow;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;

import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import model.table.TableModel;
import application.CommunicationModule.Communication;
import application.LoginAndAdministrationModule.PersonData;
import application.client.data.Client;
import application.parcelModule.Parcel;

public class ActiveParcelsPanel extends JPanel {

	public ActiveParcelsPanel(ClientHomePage parent, Communication c,
			PersonData p, boolean mode) {
		super();
		setSize(560, 440);
		setLayout(new BorderLayout());
		this.parent = parent;
		this.communication = c;
		this.personData = p;
		this.activeParcels = mode;
		init(activeParcels);
	}
	
	private void init(boolean mode) {
		activeParcels = mode;
		
		model = new TableModel(data, collumnNames);
		parcelTable = new JTable(model);
		parcelTable.getTableHeader().setReorderingAllowed(false);
		initData();
		setListener();
	}

	private void initData() {
		LinkedList<String[]> rows = new LinkedList<String[]>();
		ArrayList<Parcel> a;
		try {
			a = communication.getParcelsByClient(personData.getId());
			if (activeParcels) {
				for (Parcel p : a) {
					if (p.getStatus() < 4) {
						Client c = communication.getClientsByField("id",
								String.valueOf(p.getRecipient())).get(0);
						String[] row = { String.valueOf(p.getParcelID()),
								p.getDate(),
								c.getFirstName() + " " + c.getLastName(),
								String.valueOf(p.getPrice()),
								communication.getParcelStatus(p.getParcelID()) };
						rows.add(row);
						
					}
				}data = rows.toArray(new String[5][rows.size()]);
				add(parcelTable.getTableHeader(), BorderLayout.PAGE_START);
				add(parcelTable, BorderLayout.CENTER);			
				if (rows.size() == 0) {
					remove(parcelTable);
				}
				repaint();
				validate();
			} else {
				for (Parcel p : a) {
					if (p.getStatus() == 4) {
						Client c = communication.getClientsByField("id",
								String.valueOf(p.getRecipient())).get(0);
						String[] row = { String.valueOf(p.getParcelID()),
								p.getDate(),
								c.getFirstName() + " " + c.getLastName(),
								String.valueOf(p.getPrice()),
								communication.getParcelStatus(p.getParcelID()) };
						rows.add(row);
						
					}
				}data = rows.toArray(new String[5][rows.size()]);
				add(parcelTable.getTableHeader(), BorderLayout.PAGE_START);
				add(parcelTable, BorderLayout.CENTER);			
				if (rows.size() == 0) {
					remove(parcelTable);
				}
				repaint();
				validate();
			}
			
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
	}

	public void refresh(boolean mode) {
		remove(parcelTable);
		repaint();
		validate();
		init(mode);
	}

	private void setListener() {
		parcelTable.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					EventQueue.invokeLater(new Runnable() {
						public void run() {
							ShowDetails page = null;
							try {
								page = new ShowDetails(
										parent,
										communication,
										personData,
										communication.getParcelById(Integer.parseInt(String.valueOf(parcelTable
												.getValueAt(parcelTable
														.getSelectedRow(), 0)))));
							} catch (NumberFormatException e) {
								
								e.printStackTrace();
							} catch (SQLException e) {
								
								e.printStackTrace();
							}
							page.setVisible(true);
							parent.setVisible(false);
						}
					});
				}
			}
		});
	}

	private DefaultTableModel model;
	private JTable parcelTable;
	private String[] collumnNames = { "ID", "Data nadania", "Imię i nazwisko Odbiorcy",
			"Cena", "Status" };
	private String[][] data;
	private ClientHomePage parent;
	private Communication communication;
	private PersonData personData;
	private boolean activeParcels;
}
